package com.usian.skill.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.skill.mapper.GoodsMapper;
import com.usian.skill.pojo.Goods;
import com.usian.skill.service.GoodsService;
import com.usian.skill.vo.GoodsDetailVo;
import com.usian.skill.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public List<GoodsVo> findAll() {
        //从Redis中获取数据
        List list = JSON.parseArray(stringRedisTemplate.opsForValue().get("goods"),GoodsVo.class);

        return list;
    }

    @Override
    public GoodsVo findById(long goodsId) {
        return JSON.parseObject(stringRedisTemplate.opsForValue().get(goodsId + ""),GoodsVo.class);
    }

    @Override
    public List<GoodsVo> findAllByDB() {
        return goodsMapper.findAll();
    }
    //去数据库中查询商品
    @Override
    public GoodsVo getGoodsIdDB(long goodsId) {
        return goodsMapper.findById(goodsId);
    }
}
